// shop/goods/index.js
var home = require("../../utils/util.js");
var url = home.home;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList: [],
    bannerData: {
        indicatorDots: false,
        vertical: false,
        autoplay: false,
        interval: 2000,
        duration: 500,
        indicator_color: '#fff',
        indicator_active_color: 'rgba(255, 83, 60, 1)',
    },
    bannerNum:1,
    cur:1,
    goodsData:{},
    height:'',
    serveList:[],
    bgcolor: home.getKey('bgcolor'),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let goodsData = JSON.parse(options.info);
    let bannerNum = 0;
    let bannerList = [];
    let cur = 0;
    if (goodsData.projectPic != null) {
      bannerList = goodsData.projectPic.split(',');
      bannerNum = bannerList.length;
      for (let i = 0; i < bannerList.length; i++) {
        bannerList[i] = 'https://kyz.fbhkj.com//'+bannerList[i];
      }
      cur = 1;
    }
    if (goodsData.detailsUrl != null) {
      let detailsUrl = goodsData.detailsUrl.split(',');
      for (let i = 0; i < detailsUrl.length; i++) {
        detailsUrl[i] = 'https://kyz.fbhkj.com//'+detailsUrl[i];
      }
      goodsData.detailsUrl = detailsUrl;
    }
    goodsData.num = 0;
    this.setData({goodsData:goodsData,bannerList:bannerList,bannerNum:bannerNum,cur:cur});
  },

  getInfo(){
    let that = this;
    wx.request({
      url: url + 'app/projectDetail',
      data: {
        storeId:home.getKey('storeId'),
        projectId:that.data.goodsData.id
      },
      method: 'POST',
      header: {
        "content-type": "application/json", // 默认
      },
      success: function (res) {
        if (res.data.code == 200) {
          let serveList = [];
          for (let i = 0; i < res.data.result.serveList.length; i++) {
            let json = {};
            json.title = res.data.result.serveList[i].serveName;
            json.num = res.data.result.serveList[i].serveNum;
            serveList.push(json);
          }
          for (let i = 0; i < res.data.result.shopList.length; i++) {
            let json = {};
            json.title = res.data.result.shopList[i].shopName;
            json.num = res.data.result.shopList[i].shopNum;
            serveList.push(json);
          }
          that.setData({
            serveList:serveList,
          })
        }
      }
    })
  },

  /**
   * banner切换
   */
  onChange(e){
    this.setData({
      cur:e.detail.current+1
    });
  },

  toCart() {
    wx.switchTab({
      url: '../../pages/car/index',
    });
  },

  //加入购物车
  addCar() {
    let that = this;
    let createTime = home.js_date_time(new Date() / 1000);
    let goodsData = this.data.goodsData
    if (goodsData.cartNum >= goodsData.projectNum) {
      home.wxMessage('库存不足','error');
      return false;
    }
    wx.request({
      url: url + 'shopprojectcart/ShopProjectCart/add',
      data: {
        memberId:home.getKey('userId'),
        storeId:home.getKey('storeId'),
        createBy:'system',
        createTime:createTime,
        updateBy:'',
        updateTime:'',
        sysOrgCode:'',
        qty:1,
        projectId:goodsData.id,
        projectName:goodsData.projectName,
        projectSpec:goodsData.itemDescription,
        projectPic:goodsData.projectPic,
        price:goodsData.projectPrice,
      },
      method: 'POST',
      header: {
        "content-type": "application/json", // 默认
      },
      success: function (res) {
        let msg = '';
        let icon = '';
        if (res.data.code == 200) {
          msg = '添加成功';
          icon = 'success';
          goodsData.cartNum += 1;
          that.setData({
            goodsData: goodsData
          });
        } else {
          msg = '添加失败';
          icon = 'none';
        }
        wx.showToast({
          title: msg,
          icon: icon
        })
      }
    })
  },

  //跳转确认订单页面
  toOrder() {
    let goodsData = this.data.goodsData;
    let goods = {};
    goods.goodsId = goodsData.id;
    goods.commodityPic = goodsData.projectPic;
    goods.price = goodsData.projectPrice;
    goods.qty = 1;
    goods.commoditySpec = goodsData.itemDescription;
    goods.commodityName = goodsData.projectName;
    goods.type = 2;
    let data = []; 
    data.push(goods);
    wx.navigateTo({
      url: '../../user/order/info?data=' + JSON.stringify(data),
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height:res.windowWidth
        })
      }
    });
    this.getInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})